# coding=utf-8

import akshare as ak 
import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
import datetime 

class get_akshare():
    def __init__(self):
        self.set_print_format()
        self.time()
        pass
    
    def __del__(self):
        pass 

    def set_print_format(self):
        pd.set_option('display.unicode.ambiguous_as_wide', True)
        pd.set_option('display.unicode.east_asian_width', True)
        pd.set_option('display.width', 180)   
        pd.set_option("display.max_rows",None) 
        pd.set_option("display.max_columns",None)      

    def time(self):
        self.today = datetime.date.today()
        self.oneday = datetime.timedelta(days=1)
        self.yesterday = self.today -self.oneday
        self.hour   = datetime.datetime.now().hour
        if self.hour > 17 :
            self.date = self.today
        else :
            self.date = self.yesterday

        self.today_str = str(self.today).replace("-","")
        self.yesterday_str = str(self.yesterday).replace("-","")
        self.hour_str = str(self.hour)
        self.date_str = str(self.date).replace("-","")

        print("获取日期：" + self.today_str +":"+ self.hour_str)
        #print(self.date_str)
        #print(self.yesterday_str)
        #print(self.hour)        

    def sh_summary(self):
        print("***********上交所总貌*************")
        stock_sse_summary_df = ak.stock_sse_summary()
        print(stock_sse_summary_df)
    
    def sz_summary(self):
        print("***********深交所总貌*************")
        stock_szse_summary_df = ak.stock_szse_summary(date=self.date_str)
        print(stock_szse_summary_df)    

    def bid_ask_em(self,code):
        print("***********东财实时行情数据:"+str(code)+"*************")
        stock_bid_ask_em_df = ak.stock_bid_ask_em(symbol=str(code))
        print(stock_bid_ask_em_df)

    def bid_zh_a_hist(self,code,start_date):
        print("***********东财历史行情数据:"+str(code)+"*************")
        stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=str(code), period="daily", start_date=start_date, end_date=self.date_str, adjust="hfq")
        #print(stock_zh_a_hist_df.shape)
        #stock_zh_a_hist_df.to_excel(str(code) + "_" + start_date + "_" + self.date_str + ".xlsx",index = False)
        return stock_zh_a_hist_df

    def all_spot_em(self):
        print("***********东财实时行情数据所有股票*************")
        stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
        print(stock_zh_a_spot_em_df)

    def bj_spot_em(self):
        print("***********东财实时行情数据北交所股票*************")
        stock_bj_a_spot_em_df = ak.stock_bj_a_spot_em()
        print(stock_bj_a_spot_em_df)

    def account_statistics_em(self):
        print("***********股票账户统计月度*************")
        stock_account_statistics_em_df = ak.stock_account_statistics_em()
        print(stock_account_statistics_em_df)
    
    def fund_flow_individual(self):
        print("***********个股资金流向*************")
        stock_fund_flow_individual_df = ak.stock_fund_flow_individual(symbol="即时")
        print(stock_fund_flow_individual_df)

    def fund_flow_individual(self):
        print("***********个股资金流向*************")
        stock_fund_flow_individual_df = ak.stock_fund_flow_individual(symbol="即时")
        print(stock_fund_flow_individual_df)

    def fund_flow_big_deal(self):
        print("***********大单追踪*************")
        stock_fund_flow_big_deal_df = ak.stock_fund_flow_big_deal()
        print(stock_fund_flow_big_deal_df)   
 
    def demo(self):
        x = np.linspace(-2*np.pi,2*np.pi,1000)
        y1 = np.sin(x)
        y3 = (1/3.0)*np.sin(3*x)
        y5 = (1/5.0)*np.sin(5*x)
        y7 = (1/7.0)*np.sin(7*x)
        y = y1 + y3 + y5 + y7
        fig = plt.figure()
        plt.plot(x,y)
        plt.show()

    def demo_601069(self):
        hist = self.bid_zh_a_hist(601069,"20231218")
        hist = hist[["开盘","收盘","最高","最低","振幅","涨跌幅"]]
        hist.columns = ["open","close","high","low","amp","percent"]
        
        hist["cash"] = 1000000.0
        hist["stock"] = 0.0
        hist["value"] = 1000000.0
        hist["stock_percent"] = 1.0 
        hist["position"] = 0

        for index,row in hist.iterrows():
            hist.loc[index,"value"   ] = hist.loc[index,"cash"] + hist.loc[index,"stock"] * hist.loc[index,"close"]
            hist.loc[index,"stock_percent"] = (hist.loc[index,"stock"] * hist.loc[index,"close"] * 100) / hist.loc[index,"value"] 
            print(hist.loc[index,"value"   ])

            if hist.loc[index,"stock_percent"]  < 15 :
                hist.loc[index,"position"] = 0
            elif hist.loc[index,"stock_percent"]  < 35 :
                hist.loc[index,"position"] = 1
            elif hist.loc[index,"stock_percent"]  < 65 :
                hist.loc[index,"position"] = 2
            elif hist.loc[index,"stock_percent"]  < 90 :
                hist.loc[index,"position"] = 3
            else :
                hist.loc[index,"position"] = 4

            if hist.loc[index,"position"] == 0:
                if hist.loc[index,"percent"]  < -5:
                    hist.loc[index+1,"stock"]  = (hist.loc[index,"cash"] / 4.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] * 0.75 
                    pass 
                elif hist.loc[index,"percent"] < -2:
                    hist.loc[index+1,"stock"]  = (hist.loc[index,"cash"] / 4.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] * 0.75 
                    pass 
                elif hist.loc[index,"percent"] > 2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                elif hist.loc[index,"percent"] > 5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                else :
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 


            elif hist.loc[index,"position"] == 1:
                if hist.loc[index,"percent"]  < -5:
                    hist.loc[index+1,"stock"]  =  hist.loc[index,"stock"] + (hist.loc[index,"cash"] / 3.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] * 0.6667 
                    pass 
                elif hist.loc[index,"percent"] < -2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] + (hist.loc[index,"cash"] / 3.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] * 0.6667 
                    pass 
                elif hist.loc[index,"percent"] > 2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                elif hist.loc[index,"percent"] > 5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                else :
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 

            elif hist.loc[index,"position"] == 2:
                if hist.loc[index,"percent"]  < -5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] + (hist.loc[index,"cash"] / 2.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] * 0.5 
                    pass 
                elif hist.loc[index,"percent"] < -2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] + (hist.loc[index,"cash"] / 2.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] * 0.5 
                    pass 
                elif hist.loc[index,"percent"] > 2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                elif hist.loc[index,"percent"] > 5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] / 2.0
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] + (hist.loc[index,"stock"] * hist.loc[index,"close"] / 2.0)
                    pass 
                else :
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                pass 
            
            elif hist.loc[index,"position"] == 3:
                if hist.loc[index,"percent"]  < -5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] + (hist.loc[index,"cash"] / 1.0) / hist.loc[index,"close"]
                    hist.loc[index+1,"cash"]   = 0
                    pass 
                elif hist.loc[index,"percent"] < -2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                elif hist.loc[index,"percent"] > 2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] * 0.6667
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] + (hist.loc[index,"stock"] * hist.loc[index,"close"] / 3.0)
                    pass 
                elif hist.loc[index,"percent"] > 5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] * 0.6667
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] + (hist.loc[index,"stock"] * hist.loc[index,"close"] / 3.0)
                    pass 
                else :
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                pass 
            elif hist.loc[index,"position"] == 4:
                if hist.loc[index,"percent"]  < -5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                elif hist.loc[index,"percent"] < -2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
                elif hist.loc[index,"percent"] > 2:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] * 0.75
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] + (hist.loc[index,"stock"] * hist.loc[index,"close"] / 4.0)
                    pass 
                elif hist.loc[index,"percent"] > 5:
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"] * 0.75
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] + (hist.loc[index,"stock"] * hist.loc[index,"close"] / 4.0)
                    pass 
                else :
                    hist.loc[index+1,"stock"]  = hist.loc[index,"stock"]
                    hist.loc[index+1,"cash"]   = hist.loc[index,"cash"] 
                    pass 
            
        hist.to_excel("601069.xlsx",index = False)

if __name__ == "__main__":
    gak = get_akshare();
    #gak.sh_summary();
    #gak.sz_summary();
    #gak.bid_ask_em(430139);
    #gak.all_spot_em()
    #gak.bj_spot_em()
    #gak.account_statistics_em()
    #gak.fund_flow_individual()
    #gak.fund_flow_big_deal()]
    #gak.demo()
    #gak.bid_zh_a_hist(601069,"20150122")
    gak.demo_601069()
    pass 
